#!/bin/env python3
# -*- coding: utf-8 -*- (for python2.x)

import logging
import datetime
from PyQt5.QtWidgets import QMainWindow, QWidget, QMessageBox,\
        QTextEdit, QLineEdit, QPushButton, QLabel, QFileDialog,\
        QGridLayout, QHBoxLayout, QVBoxLayout
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPalette, QColor, QFont
import image_viewer


class MainWindow(QMainWindow):
    WINDOW_WIDTH_DEF    = 800
    WINDOW_HEIGHT_DEF   = 640
    EDITOR_FONT         = 'Monospace'
    EDITOR_FONT_SIZE    = 8

    file_path = './images/04-1.bmp'

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.log = logging.getLogger('root.MainWindow')
        self.img_viewer = image_viewer.ImageViewer()
        self.resize(self.WINDOW_WIDTH_DEF, self.WINDOW_HEIGHT_DEF)
        self.setWindowTitle('ImgAnaPy')
        self.create_menu_bar()
        self.create_status_bar()
        self.create_central()

    def load_analyzer(self):
        self.log.info('load_analyzer')

    def unload_analyzer(self):
        self.log.info('unload_analyzer')

    def truncate_log_file(self):
        self.log.info('truncate_log_file')

    def log_toggle(self):
        self.log.info('log_toggle')

    def log_clear(self):
        self.log.info('log_clear')

    def reset_size(self):
        self.resize(self.WINDOW_WIDTH_DEF, self.WINDOW_HEIGHT_DEF)

    def about(self):
        QMessageBox.about(self, 'About', 'test')

    def about_qt(self):
        QMessageBox.aboutQt(self)

    def create_menu_bar(self):
        mb = self.menuBar()
        fil = mb.addMenu('&File')
        fil.addAction('&Load library').triggered.connect(self.load_analyzer)
        fil.addAction('&Unload library').triggered.connect(self.unload_analyzer)
        fil.addAction('&Truncate log file').triggered.connect(self.truncate_log_file)
        fil.addAction('E&xit').triggered.connect(self.close)
        win = mb.addMenu('&Window')
        win.addAction('Log &toggle').triggered.connect(self.log_toggle)
        win.addAction('Log &clear').triggered.connect(self.log_clear)
        win.addAction('&Reset size').triggered.connect(self.reset_size)
        hlp = mb.addMenu('&Help')
        hlp.addAction('&About').triggered.connect(self.about)
        hlp.addAction('About&Qt').triggered.connect(self.about_qt)

    def create_status_bar(self):
        self.status_bar = self.statusBar()
        self.status_bar_time_label = QLabel()
        self.status_bar.addPermanentWidget(self.status_bar_time_label)
        tmr = QTimer(self)
        tmr.timeout.connect(self.status_bar_update_time)
        tmr.start(1000)

    def status_bar_update_time(self):
        self.status_bar_time_label.setText(str(datetime.datetime.now()))

    def resizeEvent(self, evn):
        self.status_bar.showMessage('resize:%dx%d'
                % (self.width(), self.height()), 3000)

    def create_central(self):
        # 操作面板按钮
        opn = QPushButton('&Open')
        opn.setFixedWidth(opn.fontMetrics().width(opn.text()))
        opn.pressed.connect(self.open_file)
        rld = QPushButton('&Reload')
        rld.pressed.connect(self.open_file)
        uld = QPushButton('&Unload')
        uld.pressed.connect(self.open_file)
        ana = QPushButton('&Analyze')
        ana.pressed.connect(self.open_file)
        sto = QPushButton('&Stop')
        sto.pressed.connect(self.open_file)
        sto.setDisabled(True)
        # 文件地址栏
        self.editor_file_path = QLineEdit(self.file_path)
        self.editor_file_path.setPlaceholderText('path of image file.')
        self.editor_file_path.setTextMargins(0, 0, opn.width(), 0)
        lbl_path = QLabel('&Path:')
        lbl_path.setBuddy(self.editor_file_path)
        # 状态信息框
        stv = QTextEdit()
        stp = QPalette()
        stp.setColor(QPalette.Base, QColor(0xF8, 0xF8, 0xF8))
        stv.setPalette(stp)
        stv.setFont(QFont(self.EDITOR_FONT, self.EDITOR_FONT_SIZE))
        stv.setMinimumWidth(stv.fontMetrics().width('x') * 32)
        stv.setMaximumHeight(120)
        stv.setReadOnly(True)
        # 日志文本框
        lov = QTextEdit()
        lop = QPalette()
        lop.setColor(QPalette.Base, QColor(0x20, 0x20, 0x20))
        lop.setColor(QPalette.Text, QColor(0xE0, 0xE0, 0xE0))
        lov.setPalette(lop)
        lov.setFont(QFont(self.EDITOR_FONT, self.EDITOR_FONT_SIZE))
        w = lov.fontMetrics().width('x') * 100
        lov.setMinimumWidth(w)
        lov.setMaximumHeight(w * 2)
        lov.setReadOnly(True)

        # 布局设置
        lyo_mbd = QHBoxLayout()
        lyo_mbd.setContentsMargins(0, 0, 0, 0)
        lyo_mbd.setSpacing(0)
        lyo_mbd.addWidget(opn, 0, Qt.AlignRight)
        self.editor_file_path.setLayout(lyo_mbd)
        lyo_flp = QHBoxLayout()
        lyo_flp.addWidget(lbl_path)
        lyo_flp.addWidget(self.editor_file_path)
        lyo_btn = QGridLayout()
        lyo_btn.setContentsMargins(8, 8, 8, 8)
        lyo_btn.setSpacing(4)
        lyo_btn.addWidget(rld, 0, 0)
        lyo_btn.addWidget(uld, 0, 1)
        lyo_btn.addWidget(ana, 1, 0)
        lyo_btn.addWidget(sto, 1, 1)
        lyo_ctl = QHBoxLayout()
        lyo_ctl.setContentsMargins(0, 0, 0, 0)
        lyo_ctl.setSpacing(2)
        lyo_ctl.addWidget(stv)
        lyo_ctl.addLayout(lyo_btn)
        lyo_mai = QVBoxLayout()
        lyo_mai.setContentsMargins(0, 0, 0, 0)
        lyo_mai.setSpacing(2)
        lyo_mai.addLayout(lyo_flp)
        lyo_mai.addLayout(lyo_ctl)
        lyo_mai.addWidget(self.img_viewer)
        lyo_top = QHBoxLayout()
        lyo_top.setContentsMargins(4, 4, 4, 4)
        lyo_top.setSpacing(2)
        lyo_top.addLayout(lyo_mai)
        lyo_top.addWidget(lov)
        cen = QWidget()
        cen.setLayout(lyo_top)
        self.setCentralWidget(cen)
        rld.setFocus()

        self.img_viewer.setMouseTracking(True)
        cen.setMouseTracking(True)
        self.setMouseTracking(True)
        self.update_state()

    def open_file(self):
        fn = QFileDialog.getOpenFileName(self, 'open image file',
                self.file_path, 'Image files(*)')
        if len(fn[0]) == 0:
            return
        self.file_path = fn[0]
        self.editor_file_path.setText(self.file_path)

    def update_state(self):
        pass

